package com.cy.detectsys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.EnumerationIter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.detectsys.entity.DetectTarget;
import com.cy.detectsys.entity.DetectTargetIp;
import com.cy.detectsys.service.DetectTargetIpService;
import com.cy.detectsys.mapper.DetectTargetIpMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【detect_target_ip】的数据库操作Service实现
 * @createDate 2024-05-23 18:32:39
 */
@Service
public class DetectTargetIpServiceImpl extends ServiceImpl<DetectTargetIpMapper, DetectTargetIp>
        implements DetectTargetIpService {

    @Override
    public HashMap selectByTargetName(String targetNameEn) {

        List<DetectTargetIp> list = baseMapper.selectDomainsAndIps(targetNameEn);


        List<String> domains = list.stream().map(item -> item.getTargetDomain()).filter(detectTargetIp -> detectTargetIp != null).collect(Collectors.toList());
        List<String> ips = list.stream().map(item -> item.getTargetIp()).filter(detectTargetIp -> detectTargetIp != null).collect(Collectors.toList());
        HashMap<String, List<String>> result = new HashMap<>();

        result.put("domains", domains);
        result.put("ips", ips);

        return result;
    }

    @Override
    public void updateByDetectTarget(DetectTarget detectTarget) {
        List<String> adddomains = detectTarget.getAdddomains();
        List<String> addips = detectTarget.getAddips();
        List<String> deletedomains = detectTarget.getDeletedomains();
        List<String> deleteips = detectTarget.getDeleteips();

        String targetEnglishName = detectTarget.getKyNetTargetEnglishName();

        if (CollectionUtil.isNotEmpty(deletedomains)) {
            QueryWrapper<DetectTargetIp> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("target_domain", deletedomains);
            queryWrapper.eq("target_name", targetEnglishName);
            baseMapper.delete(queryWrapper);

        }
        if (CollectionUtil.isNotEmpty(deleteips)) {
            QueryWrapper<DetectTargetIp> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("target_ip", deleteips);
            queryWrapper1.eq("target_name", targetEnglishName);
            baseMapper.delete(queryWrapper1);
        }

        for (String adddomain : adddomains) {

            DetectTargetIp detectTargetIp = new DetectTargetIp();
            detectTargetIp.setTargetId(detectTarget.getId());
            detectTargetIp.setTargetDomain(adddomain);
            detectTargetIp.setDeleted(0);
            detectTargetIp.setTargetName(targetEnglishName);
            baseMapper.insert(detectTargetIp);
        }

        for (String addip : addips) {

            DetectTargetIp detectTargetIp = new DetectTargetIp();
            detectTargetIp.setTargetId(detectTarget.getId());
            detectTargetIp.setTargetIp(addip);
            detectTargetIp.setDeleted(0);
            detectTargetIp.setTargetName(targetEnglishName);
            baseMapper.insert(detectTargetIp);
        }
    }
}




